package entity

const CheckHeartbeatScript = `#!/bin/bash

timestamp=$(cat /var/log/toneagent/.heartbeat)
now=$(date +%s)
if [ $(($now - $timestamp)) -gt 600 ]; then
  echo 'no heartbeat, restart service now'
  systemctl restart toneagent
fi
`

const UpgradeScript = `#!/bin/bash

if grep -q "ID=debian" /etc/os-release || grep -q "ID=ubuntu" /etc/os-release; then
    os=Debian
	apt-get install -y jq >/dev/null
else
    os=Linux
	yum install -y jq >/dev/null
fi
echo $os system
arch=$(uname -m)

proxy_domain=https://tone-agent.openanolis.cn
assign_version=${1:-latest}

# 接收版本参数 new|stable
if [ "$assign_version" = "latest" ]; then
    is_new=1
    is_stable=""
	version=""
elif [ "$assign_version" = "stable" ]; then
    is_new=""
    is_stable=1
	version=""
else
	is_new=""
    is_stable=""
	version=$assign_version
fi

# 调用API获取需要安装的版本
api_url="$proxy_domain/v1/agent/version/manage?is_deleted=0&os=$os&arch=$arch&is_new=$is_new&is_stable=$is_stable&version=$version"
api_response=$(curl -s "$api_url")
if ! echo "$api_response" | jq -e . >/dev/null 2>&1; then
    echo "获取版本失败:$api_response"
	exit
fi

array_length=$(echo "$api_response" | jq '.data | length')
# 判断数组长度是否为0
if [ "$array_length" -eq 0 ]; then
    echo "无可用版本，请检查指定版本是否正确（toneagent -l 可查看可用版本号）"
	exit
fi

# 解析API响应，获取版本链接
version_link=$(echo "$api_response" | jq -r '.data[0].link')
#version="$version_link" | awk -F'/' '{print $NF}'
version=$(basename "$version_link")

if [ $? -eq 0 ]; then
    echo "The $assign_version version is $version"
else
	echo "Upgrade failed"
	exit
fi

echo -e "\e[1;33mUninstalling the old version of toneagent\e[0m"
if [ "$os" = "Debian" ]; then
    dpkg -r toneagent
else
    rpm -e toneagent
fi

echo -e "\e[1;33mUpgrade toneagent to $version\e[0m"

wget $version_link

if [ "$os" = "Debian" ]; then
    dpkg -i $version
else
    yum install -y ./$version
fi

if [ $? -eq 0 ]; then
    echo "Upgrade success"
else
	echo "Upgrade failed($?)"
fi

# 输出当前版本
toneagent -v
`

const ListVersionScript = `#!/bin/bash

if grep -q "ID=debian" /etc/os-release || grep -q "ID=ubuntu" /etc/os-release; then
    os=Debian
	apt-get install -y jq >/dev/null
else
    os=Linux
	yum install -y jq >/dev/null
fi

arch=$(uname -m)

proxy_domain=https://tone-agent.openanolis.cn
oss_head="https://anolis-service-pub.oss-cn-zhangjiakou.aliyuncs.com/biz-resource/tone/rpms/"

# 调用API获取可安装的版本列表
api="$proxy_domain/v1/agent/version/manage?is_deleted=0&os=$os&arch=$arch&page_size=100&page_num=1"
response=$(curl -s "$api")

versions=$(echo "$response" | jq -r '.data[].version')
links=$(echo "$response" | jq -r '.data[].link')
stables=$(echo "$response" | jq -r '.data[].is_stable')

IFS=$'\n'
versionArray=($versions)
linkArray=($links)
stableArray=($stables)

echo "+++++++++++++++++++++++++++++++++++++++"
echo "| Version | OS     | Arch    | Stable |"
echo "+++++++++++++++++++++++++++++++++++++++"
for ((i=0; i<${#versionArray[@]}; i++)); do
	# link=$(echo ${linkArray[i]} | sed "s|^$oss_head||")
	stable=${stableArray[i]}
	isstable=$( [ "$stable" = "true" ] && echo " yes " || echo "" )
  echo "| ${versionArray[i]}   | $(printf "%-6s" "$os") | $(printf "%-7s" "$arch") | $(printf "%-6s" "$isstable") |"
done
echo "+++++++++++++++++++++++++++++++++++++++"
`
